﻿/*
 * 版 本 v1.0
 * Copyright (c) 2013-2018 广州同慧信息技术有限公司
 * 创建人：ftquan
 * 日 期：2017.05.24
 * 描 述：lr-uploader 表单附件选择插件
 */
(function ($, learun) {
    "use strict";

    $.lrUploader = {
        init: function ($self) {
            var dfop = $self[0]._lrUploader.dfop;
            $.lrUploader.initRender($self, dfop);
        },
        initRender: function ($self, dfop) {
            $self.attr('type', 'lr-Uploader').addClass('lrUploader-wrap');
            var $wrap = $('<div class="lrUploader-output"><span style="margin-right:20px;">缺少说明:</span><input type="text" style="border: none; width: 180px; border-bottom: solid 1px #7F9DB9;color:blue;"/></div>');

            var $btnGroup = $('<div class="lrUploader-btn-group"></div>');
            var $uploadBtn = $('<a id="lrUploader_uploadBtn_' + dfop.id + '" class="btn btn-success lrUploader-input-btn">上传</a>');
            var $downBtn = $('<a id="lrUploader_downBtn_' + dfop.id + '" class="btn btn-danger lrUploader-input-btn">查看</a>');
            var $viewBtn = $('<a id="lrUploader_viewBtn_' + dfop.id + '" class="btn btn-primary lrUploader-input-btn">预览</a>');

            $self.append($wrap);
            var w = 0;
            if (dfop.isUpload) {
                $btnGroup.append($uploadBtn);
                w += 57;
            }
            if (dfop.isDown) {
                $btnGroup.append($downBtn);
                w += 57;
            }
            if (dfop.isView) {
                $btnGroup.append($viewBtn);
                w += 57;
            }
            $uploadBtn.on('click', $.lrUploader.openUploadForm);
            $downBtn.on('click', $.lrUploader.openDownForm);
            $viewBtn.on('click', $.lrUploader.openViewForm);

            $self.append($btnGroup);
            $self.css({'padding-right': w});
        },
        openUploadForm: function () {
            var $btn = $(this);
            var $self = $btn.parents('.lrUploader-wrap');
            var dfop = $self[0]._lrUploader.dfop;
            learun.layerForm({
                id: dfop.id,
                title: dfop.placeholder,
                url: config.webportal_server + 'pages/utility/uploadForm2.html?keyVaule=' + dfop.value + "&extensions=" + dfop.extensions + '&isInvoice=' + dfop.isInvoice + '&processId=' + dfop.processId + '&access_token=' + config.getToken().access_token,
                width: 600,
                height: 400,
                maxmin: true,
                btn: null,
                end: function () {
                    learun.httpAsyncGet(config.base_server() + 'files/list?folderId=' + dfop.value + '&access_token=' + config.getToken().access_token, function (res) {
                        if (res.resp_code == 200) {
                            $('#' + dfop.id).find('.lrUploader-output').empty();
                            $('#' + dfop.id).find('.lrUploader-output').attr("data-id", dfop.value);
                            for(var i=0;i<res.data.length;i++) {
                                var item = res.data[i];
                                var $item = $('<div class="lr-form-file-queue-item" id="lr_filequeue_' + item.id + '" data-id="'+item.folderId+'"></div>');
                                $item.append('<div class="lr-file-image"><img src="/assets/plugins/images/filetype/' + item.ext + '.png"></div>');


                                $item.append('<div>');
                                $item.append('<span class="lr-file-name">' + item.name + '</span>');
                                $item.append('<span class="lr-file-time">' + item.updateTime + '(' + item.updateUsername + ')</span>');
                                $item.append('</div>');

                                $item.append('<div class="lr-tool-bar"><i class="fa fa-eye" title="预览"  data-value="' + item.id + '" ></i><i class="fa fa-cloud-download" title="下载"  data-value="' + item.id + '" ></i></div>');

                                $item.find('.lr-tool-bar .fa-cloud-download').on('click', function () {
                                    var fileId = $(this).attr('data-value');
                                    $.lrUploader.DownFile(fileId);
                                });
                                $item.find('.lr-tool-bar .fa-eye').on('click', function () {
                                    var fileId = $(this).attr('data-value');
                                    $.lrUploader.openViewForm(fileId);
                                });
                                $('#' + dfop.id).find('.lrUploader-output').append($item);
                            }
                        }
                    });
                }
            });
        },
        openDownForm: function () {
            var $btn = $(this);
            var $self = $btn.parents('.lrUploader-wrap');
            var dfop = $self[0]._lrUploader.dfop;
            learun.layerForm({
                id: dfop.id,
                title: dfop.placeholder,
                url: config.webportal_server + 'pages/utility/downForm.html?keyVaule=' + dfop.value + '&access_token=' + config.getToken().access_token,
                width: 600,
                height: 400,
                maxmin: true,
                btn: null
            });
        },
        openViewForm: function (fileId) {
            if(!fileId) {
                var $btn = $(this);
                var $self = $btn.parents('.lrUploader-wrap');
                var dfop = $self[0]._lrUploader.dfop;
                fileId = dfop.value;
            }
            learun.layerForm({
                id: 'PreviewForm',
                title: '文件预览',
                url: config.webportal_server + 'pages/utility/previewFile.html?keyVaule=' + fileId + '&access_token=' + config.getToken().access_token,
                width: 1080,
                height: 850,
                btn: null
            });
        },
        // 下载文件
        DownFile: function (fileId) {
            learun.download({
                url: config.base_server() + 'files/download?id=' +  fileId + '&access_token=' + config.getToken().access_token,
                param: {fileId: fileId},
                method: 'POST'
            });
        }
    };

    $.fn.lrUploader = function (op) {
        var $this = $(this);
        if (!!$this[0]._lrUploader) {
            return $this;
        }
        var dfop = {
            placeholder: '上传附件',
            isUpload: true,
            isDown: true,
            extensions: '',
            processId: '',
            isInvoice: 0
        }

        $.extend(dfop, op || {});
        dfop.id = $this.attr('id');
        if (!dfop.value) {
            dfop.value = learun.newGuid();
        }

        $this[0]._lrUploader = {dfop: dfop};
        $.lrUploader.init($this);
    };

    $.fn.lrUploaderSet = function (value) {
        var $self = $(this);
        var dfop = $self[0]._lrUploader.dfop;
        dfop.value = value;
//        learun.httpAsyncGet(config.base_server() + 'files/getNames?folderId=' + dfop.value + '&access_token=' + config.getToken().access_token, function (res) {
//            if (res.resp_code == 200) {
//                $('#' + dfop.id).find('.lrUploader-input').text(res.resp_msg);
//            }
//        });
        learun.httpAsyncGet(config.base_server() + 'files/list?folderId=' + dfop.value + '&access_token=' + config.getToken().access_token, function (res) {
            if (res.resp_code == 200) {
                $('#' + dfop.id).find('.lrUploader-output').empty();
                $('#' + dfop.id).find('.lrUploader-output').attr("data-id", dfop.value);
                for(var i=0;i<res.data.length;i++) {
                    var item = res.data[i];
                    var $item = $('<div class="lr-form-file-queue-item" id="lr_filequeue_' + item.id + '"></div>');
                    $item.append('<div class="lr-file-image"><img src="/assets/plugins/images/filetype/' + item.ext + '.png"></div>');


                    $item.append('<div>');
                    $item.append('<span class="lr-file-name">' + item.name + '</span>');
                    $item.append('<span class="lr-file-time">' + item.updateTime + '(' + item.updateUsername + ')</span>');
                    $item.append('</div>');

                    $item.append('<div class="lr-tool-bar"><i class="fa fa-eye" title="预览"  data-value="' + item.id + '" ></i><i class="fa fa-cloud-download" title="下载"  data-value="' + item.id + '" ></i></div>');

                    $item.find('.lr-tool-bar .fa-cloud-download').on('click', function () {
                        var fileId = $(this).attr('data-value');
                        $.lrUploader.DownFile(fileId);
                    });
                    $item.find('.lr-tool-bar .fa-eye').on('click', function () {
                        var fileId = $(this).attr('data-value');
                        $.lrUploader.openViewForm(fileId);
                    });
                    $('#' + dfop.id).find('.lrUploader-output').append($item);
                }
            }
        });
    }

    $.fn.lrUploaderReset = function () {
        var $self = $(this);
        var dfop = $self[0]._lrUploader.dfop;
        dfop.value = null;
        $('#' + dfop.id).find('.lrUploader-input').text('');
    }

    $.fn.lrUploaderGet = function () {
        var $this = $(this);
        var dfop = $this[0]._lrUploader.dfop;
        return dfop.value;
    }
})(jQuery, top.learun);